<?php



namespace App\Services;


use App\Models\Customer;
use App\Models\CustomerRecords;
use App\Models\Work;
use App\Service\Service;
use Carbon\Carbon;
use Dingo\Api\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

/**
 * Class WorkService
 * @property integer $amount
 * @property integer $effect_mobile
 * @property string $remark
 * @package App\Services
 * @author thanatos thanatos915@163.com
 */
class WorkService extends Service
{

    /**
     * 创建日报
     * @author thanatos thanatos915@163.com
     */
    public static function create(Request $request)
    {
        $service = self::loadParams($request, [
            'amount' => 'required|numeric',
            'effect_mobile' => 'required|numeric',
            'remark' => 'required|string'
        ]);

        $day = Carbon::now()->startOfDay();

        // 判断今天是否已经提交过日报
        if (Work::where('day', $day)->where('user_id', get_user_id())->exists()) {
            throw new BadRequestHttpException("今日已经提交过日报了, 明天再来吧");
        }

        $dayData = static::getDayData();

        Work::create([
            'user_id' => get_user_id(),
            'amount' => $service->amount,
            'effect_mobile' => $service->effect_mobile,
            'intention' => $dayData['intention'],
            'visit' => $dayData['meet'],
            'order' => $dayData['order'],
            'day' => $day,
            'wechat' => $dayData['wechat'],
            'remark' => $service->remark
        ]);
    }

    /**
     * 获取日历数据
     * @param Request $request
     * @return array
     * @author thanatos thanatos915@163.com
     */
    public static function calendar(Request $request)
    {
        $start = Carbon::now()->startOfMonth();
        $end = Carbon::now()->endOfMonth();

        $data = Work::where('user_id', get_user_id())
            ->whereBetween('day', [$start, $end])
            ->orderBy('day', 'asc')
            ->get();


        // 循环日期
        $res = [];
        foreach ($data as $work) {
            $res[] = array_merge($work->toArray(), ['day' => $work->day->toDateString()]);
        }
        return $res;
    }

    public static function getDayData() {
        $start = Carbon::now()->startOfDay();
        $end = Carbon::now()->endOfDay();

        // 今日跟进意向客户数
        $intention = CustomerRecords::where('user_id', get_user_id())
            ->where('result', CustomerRecords::RESULT_INTENTION)
            ->whereBetween('created_at', [$start, $end])
            ->count();

        // 今日成单数
        $order = CustomerRecords::where('user_id', get_user_id())
            ->whereBetween('created_at', [$start, $end])
            ->where('result', CustomerRecords::RESULT_ORDER)
            ->count();

        // 今日微信数
        $wechat = Customer::where('follow_id', get_user_id())
            ->whereBetween('add_wechat_at', [$start, $end])
            ->count();

        // 拜访客户量
        $meet = CustomerRecords::where('user_id', get_user_id())
            ->whereBetween('created_at', [$start, $end])
            ->where('type', CustomerRecords::TYPE_MEET)
            ->count();
        return [
            'intention' => $intention,
            'order' => $order,
            'wechat' => $wechat,
            'meet' => $meet
        ];
    }

}
